home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / InputSprocket 1.0.1 / InputSprocketTest Sources / InputSprocketSimpleTest.cp next >
Encoding:
Text File  |  1996-09-12  |  22.4 KB  |  1,161 lines  |  [TEXT/CWIE]

  1. #ifndef DEBUG_DRIVER
  2.     #define DEBUG_DRIVER 0
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #include <DeskBus.h>
  9.  
  10. #include "InputSprocket.h"
  11. #include "SIOUX.h"
  12. #include <CursorDevices.h>
  13. #include <LowMem.h>
  14.  
  15. #if DEBUG_DRIVER
  16.     #include "InputSprocketDriver.h"
  17. #endif
  18.  
  19. enum
  20. {
  21.     kNeedCount = 9
  22. };
  23.  
  24. enum
  25. {
  26.     kIconSuiteID_XThrust = 128, 
  27.     kIconSuiteID_YThrust,
  28.     kIconSuiteID_ZThrust,
  29.     kIconSuiteID_Look,
  30.     kIconSuiteID_Fire,
  31.     kIconSuiteID_Thrust,
  32.     kIconSuiteID_Pause,
  33.     kIconSuiteID_Start,
  34.     kIconSuiteID_Scroll
  35. };
  36.  
  37. Boolean gIsSuspended = false;
  38. Boolean gKeyboardEnabled = false;
  39. ISpElementListReference gVirtualList = NULL;
  40. ISpElementReference gVirtualElements[kNeedCount] = {nil, nil, nil, nil, nil, nil, nil, nil, nil};
  41.       
  42.  
  43.  
  44. static ISpNeed gNeeds[kNeedCount] =
  45. {
  46.     { "\pForward Thrust",    kIconSuiteID_YThrust,     0,    kISpElementKind_Axis,        kISpElementLabel_YAxis,    0                            },
  47.     { "\pSide Thrust",         kIconSuiteID_XThrust,    0,    kISpElementKind_Axis,        kISpElementLabel_XAxis,    0                            },
  48.     { "\pVertical Thrust",     kIconSuiteID_ZThrust,     0,    kISpElementKind_Axis,        kISpElementLabel_ZAxis,    0                            },
  49.     { "\pLook",             kIconSuiteID_Look,        0,    kISpElementKind_Movement,    kISpElementLabel_None,    0                            },
  50.     { "\pFire",             kIconSuiteID_Fire,        0,    kISpElementKind_Button,        kISpElementLabel_Fire,    0                            },
  51.     { "\pThrust",             kIconSuiteID_Thrust,     0,    kISpElementKind_Button,        kISpElementLabel_Fire,    0                            },
  52.     { "\pPause",             kIconSuiteID_Pause,        0,    kISpElementKind_Button,        kISpElementLabel_None,    kISpNeedFlag_NoMultiConfig    },
  53.     { "\pStart/Stop",         kIconSuiteID_Start,     0,    kISpElementKind_Button,        kISpElementLabel_Start,    kISpNeedFlag_NoMultiConfig    },
  54.     { "\pScroll",             kIconSuiteID_Scroll,    0,    kISpElementKind_DPad,        kISpElementLabel_None,    0                            }
  55. };
  56.  
  57.  
  58. void ShowFourByte(OSType fourByte);
  59. void ShowFourByte(OSType fourByte)
  60. {
  61.     putchar(((fourByte & 0xff000000) >> 24));
  62.     putchar(((fourByte & 0x00ff0000) >> 16));
  63.     putchar(((fourByte & 0x0000ff00) >> 8));
  64.     putchar(((fourByte & 0x000000ff) >> 00));
  65. }
  66.  
  67. void ShowStr63(const Str63 &theStr);
  68. void ShowStr63(const Str63 &theStr)
  69. {
  70.     int i;
  71.     int len = theStr[0];
  72.     if (len > 63) { len = 63; }
  73.     
  74.     for(i=1; i <= len; i++)
  75.     {
  76.         putchar(theStr[i]);
  77.     }
  78. }
  79.  
  80.  
  81. void PrintEvent(ISpElementEventPtr theEvent);
  82. void PrintEvent(ISpElementEventPtr theEvent)
  83. {
  84.     ISpElementInfo info;
  85.     ISpElement_GetInfo(theEvent->element, &info);
  86.     OSStatus status;
  87.     const char* s = "•UNKNOWN•";
  88.     
  89.     printf("when = [%lu] [%lu]\n", theEvent->when.hi, theEvent->when.lo);
  90.     
  91.     switch (info.theKind)
  92.     {
  93.         case kISpElementKind_Button:
  94.             switch (theEvent->data)
  95.             {
  96.                 case 0:
  97.                     s = "released";
  98.                 break;
  99.                 
  100.                 case 1:
  101.                     s = "pressed";
  102.                 break;
  103.             }
  104.             
  105.             printf("data = 0x%lx (%s)\n", theEvent->data, s);
  106.         break;
  107.         
  108.         case kISpElementKind_DPad:
  109.             switch (theEvent->data)
  110.             {
  111.                 case kISpPadIdle:
  112.                     s = "kISpPadIdle";
  113.                 break;
  114.                 
  115.                 case kISpPadLeft:
  116.                     s = "kISpPadLeft";
  117.                 break;
  118.                 
  119.                 case kISpPadUpLeft:
  120.                     s = "kISpPadUpLeft";
  121.                 break;
  122.                 
  123.                 case kISpPadUp:
  124.                     s = "kISpPadUp";
  125.                 break;
  126.                 
  127.                 case kISpPadUpRight:
  128.                     s = "kISpPadUpRight";
  129.                 break;
  130.                 
  131.                 case kISpPadRight:
  132.                     s = "kISpPadRight";
  133.                 break;
  134.                 
  135.                 case kISpPadDownRight:
  136.                     s = "kISpPadDownRight";
  137.                 break;
  138.                 
  139.                 case kISpPadDown:
  140.                     s = "kISpPadDown";
  141.                 break;
  142.                 
  143.                 case kISpPadDownLeft:
  144.                     s = "kISpPadDownLeft";
  145.                 break;
  146.             }
  147.             
  148.             printf("data = 0x%lx (%s)\n", theEvent->data, s);
  149.         break;
  150.         
  151.         case kISpElementKind_Axis:
  152.             float temp1 = ((float) theEvent->data)/((float) 0xFFFFFFFFU);
  153.             float temp2 = 2.0*temp1-1.0;
  154.             
  155.             printf("data = 0x%lx (%f%) (%f%)\n", theEvent->data, temp1, temp2);
  156.         break;
  157.         
  158.         case kISpElementKind_Movement:
  159.             printf("data = 0x%lx\n", theEvent->data);
  160.             
  161.             ISpMovementData movementData;
  162.             if (ISpElement_GetComplexState(theEvent->element, sizeof(ISpMovementData), &movementData) == noErr)
  163.             {
  164.                 switch (movementData.direction)
  165.                 {
  166.                     case kISpPadIdle:
  167.                         s = "kISpPadIdle";
  168.                     break;
  169.                     
  170.                     case kISpPadLeft:
  171.                         s = "kISpPadLeft";
  172.                     break;
  173.                     
  174.                     case kISpPadUpLeft:
  175.                         s = "kISpPadUpLeft";
  176.                     break;
  177.                     
  178.                     case kISpPadUp:
  179.                         s = "kISpPadUp";
  180.                     break;
  181.                     
  182.                     case kISpPadUpRight:
  183.                         s = "kISpPadUpRight";
  184.                     break;
  185.                     
  186.                     case kISpPadRight:
  187.                         s = "kISpPadRight";
  188.                     break;
  189.                     
  190.                     case kISpPadDownRight:
  191.                         s = "kISpPadDownRight";
  192.                     break;
  193.                     
  194.                     case kISpPadDown:
  195.                         s = "kISpPadDown";
  196.                     break;
  197.                     
  198.                     case kISpPadDownLeft:
  199.                         s = "kISpPadDownLeft";
  200.                     break;
  201.                 }
  202.                 
  203.                 float horz = 2.0*(((float) movementData.xAxis)/((float) 0xFFFFFFFFU))-1.0;
  204.                 float vert = 2.0*(((float) movementData.yAxis)/((float) 0xFFFFFFFFU))-1.0;
  205.                 
  206.                 printf("state = (%f,%f) (%s)\n", horz, vert, s);
  207.             }
  208.         break;
  209.     }
  210.     
  211.     printf("element = 0x%lx\n", theEvent->element);
  212.     printf("refcon = %lx\n", theEvent->refCon);
  213.     
  214.     printf("label = ");
  215.     ShowFourByte(info.theLabel);
  216.     printf("\n");
  217.     
  218.     printf("kind = ");
  219.     ShowFourByte(info.theKind);
  220.     printf("\n");
  221.     
  222.     printf("string = ");
  223.     ShowStr63(info.theString);
  224.     printf("\n");
  225.     
  226.     {
  227.         UInt32 data;
  228.  
  229.         status = ISpElement_GetSimpleState(theEvent->element, &data);
  230.  
  231.         if (status == noErr) { printf("polled = %x\n",data); }
  232.     }
  233.     
  234.     printf("\n\n");
  235. }
  236.  
  237.  
  238. void PrintElementBlock(ISpElementReference *theElementReferences, UInt32 count);
  239. void PrintElementBlock(ISpElementReference *theElementReferences, UInt32 count)
  240. {
  241.     printf("count = %d\n");
  242.  
  243.     int itr;
  244.     
  245.     for(itr = 0; itr < count; itr++)
  246.     {
  247.         printf("    element #%d\n",itr);
  248.         
  249.         ISpElementInfo info;
  250.  
  251.         ISpElement_GetInfo(theElementReferences[itr], &info);
  252.  
  253.         printf("        label = ");
  254.         ShowFourByte(info.theLabel);
  255.         printf("\n");
  256.  
  257.         printf("        kind = ");
  258.         ShowFourByte(info.theKind);
  259.         printf("\n");
  260.  
  261.         printf("        string = ");
  262.         ShowStr63(info.theString);
  263.         printf("\n\n");
  264.     }
  265. }
  266.  
  267. // if whichList is NULL then we use the global list
  268. static void GetAndPrintEvents(ISpElementListReference whichList)
  269. {
  270.     OSErr err;
  271.     
  272.     if (whichList == NULL)
  273.     {
  274.         err = ISpGetGlobalElementList(&whichList);
  275.         if (err)
  276.         {
  277.             printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  278.             return;
  279.         }
  280.     }
  281.  
  282.     err = ISpElementList_Flush(whichList);
  283.     if (err)
  284.     {
  285.         printf("••• ISpElementList_Flush returned error (%ld)\n",err);
  286.         return;
  287.     }
  288.  
  289.     printf("getting events (press command to end)\n");
  290.     
  291.     while(1)
  292.     {
  293.         ISpElementEvent event;
  294.         Boolean wasEvent;
  295.  
  296.         err = ISpElementList_GetNextEvent(whichList, sizeof(event), &event, &wasEvent);
  297.         if (err)
  298.         {
  299.             printf("••• ISpElementList_GetNextEvent returned error (%ld)\n",err);
  300.             return;
  301.         }
  302.  
  303.         if (wasEvent)
  304.         {
  305.             PrintEvent(&event);
  306.         }
  307.         
  308.         KeyMap theKeys;
  309.         GetKeys(theKeys);
  310.  
  311.         if ((theKeys[1] & 0x8000))
  312.         {
  313.             break;
  314.         }
  315.         
  316.         SIOUXHandleOneEvent(nil);
  317.       }
  318. }
  319.  
  320. static void TestListOfDevices(void)
  321. {
  322.     ISpDeviceReference theDevices[100];
  323.     UInt32 deviceCount;
  324.     UInt32 deviceBufferSize = 100;
  325.     UInt32 deviceItr;
  326.     OSErr err;
  327.     
  328.     err = ISpDevices_Extract(deviceBufferSize, &deviceCount, theDevices);
  329.     if (err)
  330.     {
  331.         printf("••• ISpDevices_Extract returned error (%ld)\n",err);
  332.         return;
  333.     }
  334.     
  335.     printf("# of devices = %ld\n",deviceCount);
  336.     for(deviceItr = 0; deviceItr < deviceCount; deviceItr++)
  337.     {
  338.         printf("    device #%ld\n",deviceItr);
  339.         ISpDeviceDefinition theDfn;
  340.         
  341.         err = ISpDevice_GetDefinition(theDevices[deviceItr], sizeof(ISpDeviceDefinition), &theDfn);
  342.         if (err)
  343.         {
  344.             printf("••• ISpDevice_GetDefinition returned error (%ld)\n",err);
  345.             return;
  346.         }
  347.         
  348.         printf("        name = ");
  349.         ShowStr63(theDfn.deviceName);
  350.         printf("\n");
  351.         
  352.         printf("        class = ");
  353.         ShowFourByte(theDfn.theDeviceClass);
  354.         printf("\n");
  355.  
  356.         printf("        device identifier = ");
  357.         ShowFourByte(theDfn.theDeviceIdentifier);
  358.         printf("\n");
  359.  
  360.         printf("        permanent id = %ld\n",theDfn.permanentID);
  361.         
  362.         printf("\n\n");
  363.     }
  364. }
  365.  
  366.  
  367. static void TestListOfElements(void)
  368. {
  369.     ISpElementListReference globalList;
  370.     OSErr err;
  371.     
  372.     err = ISpGetGlobalElementList(&globalList);
  373.     if (err)
  374.     {
  375.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  376.         return;
  377.     }
  378.     
  379.     ISpElementReference theElementReferences[200];
  380.     UInt32 bufferSize = 200;
  381.     UInt32 count;
  382.  
  383.     err = ISpElementList_Extract(globalList, bufferSize, &count, theElementReferences);
  384.     if (err)
  385.     {
  386.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  387.         return;
  388.     }
  389.     
  390.     PrintElementBlock(theElementReferences, count);
  391. }
  392.  
  393.  
  394. static void TestListOfButtons(void)
  395. {
  396.     ISpElementListReference globalList;
  397.     OSErr err;
  398.     
  399.     err = ISpGetGlobalElementList(&globalList);
  400.     if (err)
  401.     {
  402.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  403.         return;
  404.     }
  405.     
  406.     ISpElementReference theElementReferences[200];
  407.     UInt32 bufferSize = 200;
  408.     UInt32 count;
  409.  
  410.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_Button, bufferSize, &count, theElementReferences);
  411.     if (err)
  412.     {
  413.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  414.         return;
  415.     }
  416.     
  417.     PrintElementBlock(theElementReferences, count);
  418. }
  419.  
  420.  
  421. static void TestListOfDPads(void)
  422. {
  423.     ISpElementListReference globalList;
  424.     OSErr err;
  425.     
  426.     err = ISpGetGlobalElementList(&globalList);
  427.     if (err)
  428.     {
  429.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  430.         return;
  431.     }
  432.     
  433.     ISpElementReference theElementReferences[200];
  434.     UInt32 bufferSize = 200;
  435.     UInt32 count;
  436.  
  437.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_DPad, bufferSize, &count, theElementReferences);
  438.     if (err)
  439.     {
  440.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  441.         return;
  442.     }
  443.     
  444.     PrintElementBlock(theElementReferences, count);
  445. }
  446.  
  447.  
  448. static void TestListOfAxes(void)
  449. {
  450.     ISpElementListReference globalList;
  451.     OSErr err;
  452.     
  453.     err = ISpGetGlobalElementList(&globalList);
  454.     if (err)
  455.     {
  456.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  457.         return;
  458.     }
  459.     
  460.     ISpElementReference theElementReferences[200];
  461.     UInt32 bufferSize = 200;
  462.     UInt32 count;
  463.  
  464.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_Axis, bufferSize, &count, theElementReferences);
  465.     if (err)
  466.     {
  467.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  468.         return;
  469.     }
  470.     
  471.     PrintElementBlock(theElementReferences, count);
  472. }
  473.  
  474.  
  475. static void TestListOfXAxes(void)
  476. {
  477.     ISpElementListReference globalList;
  478.     OSErr err;
  479.     
  480.     err = ISpGetGlobalElementList(&globalList);
  481.     if (err)
  482.     {
  483.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  484.         return;
  485.     }
  486.     
  487.     ISpElementReference theElementReferences[200];
  488.     UInt32 bufferSize = 200;
  489.     UInt32 count;
  490.  
  491.     err = ISpElementList_ExtractByLabel(globalList, kISpElementLabel_XAxis, bufferSize, &count, theElementReferences);
  492.     if (err)
  493.     {
  494.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  495.         return;
  496.     }
  497.     
  498.     PrintElementBlock(theElementReferences, count);
  499. }
  500.  
  501.  
  502. static void TestSuspend(void)
  503. {
  504.     OSErr err;
  505.     
  506.     err = ISpSuspend();
  507.     if (err)
  508.     {
  509.         printf("••• ISpSuspend returned error (%ld)\n",err);
  510.         return;
  511.     }
  512.     
  513.     gIsSuspended = true;
  514. }
  515.  
  516.  
  517. static void TestResume(void)
  518. {
  519.     OSErr err;
  520.     
  521.     err = ISpResume();
  522.     if (err)
  523.     {
  524.         printf("••• ISpResume returned error (%ld)\n",err);
  525.         return;
  526.     }
  527.     
  528.     gIsSuspended = false;
  529. }
  530.  
  531.  
  532. static void TestCreateNeeds(void)
  533. {
  534.     OSErr err;
  535.     
  536.     if (gVirtualList == NULL)
  537.     {
  538.         printf("creating virtual elements\n");
  539.         
  540.           err = ISpElement_NewVirtualFromNeeds(kNeedCount, gNeeds, gVirtualElements, 0);
  541.         if (err)
  542.         {
  543.             printf("••• ISpElement_NewVirtualFromNeeds returned error (%ld)\n",err);
  544.             return;
  545.         }
  546.         
  547.         err = ISpElementList_New(
  548.             kNeedCount,                // count
  549.             gVirtualElements,        // needs
  550.             &gVirtualList,            // virtual elements
  551.             0);                        // flags
  552.         if (err)
  553.         {
  554.             printf("••• ISpElementList_New returned error (%ld)\n",err);
  555.             return;
  556.         }
  557.               
  558.         err = ISpInit(kNeedCount,    // count
  559.                 gNeeds,                // needs
  560.                 gVirtualElements,    // virtual elements
  561.                 'ISpT',                // app
  562.                 'foob',                // sub
  563.                 0,                    // flags
  564.                 128,                // set list resource id
  565.                 0);                    // version
  566.         if (err)
  567.         {
  568.             printf("••• ISpInit returned error (%ld)\n",err);
  569.             return;
  570.         }
  571.     }
  572. }
  573.  
  574.  
  575. static void TestDestroyNeeds(void)
  576. {
  577.     OSErr err;
  578.     
  579.     if (gVirtualList != NULL)
  580.     {
  581.         err = ISpElementList_Dispose(gVirtualList);
  582.         gVirtualList = NULL;
  583.         if (err)
  584.         {
  585.             printf("••• ISpElementList_Dispose returned error (%ld)\n",err);
  586.             return;
  587.         }
  588.         
  589.          err = ISpStop();
  590.         if (err)
  591.         {
  592.             printf("••• ISpStop returned error (%ld)\n",err);
  593.             return;
  594.         }
  595.         
  596.         err = ISpElement_DisposeVirtual(kNeedCount, gVirtualElements);
  597.         if (err)
  598.         {
  599.             printf("••• ISpElement_DisposeVirtual returned error (%ld)\n",err);
  600.             return;
  601.         }
  602.     }
  603. }
  604.  
  605.  
  606. static void TestNeedsEvents(void)
  607. {
  608.     TestCreateNeeds();
  609.     
  610.     if (gVirtualList != NULL)
  611.     {
  612.         GetAndPrintEvents(gVirtualList);
  613.     }
  614. }
  615.  
  616.  
  617. static void TestConfiguration(void)
  618. {
  619.     OSErr err;
  620.     
  621.     TestCreateNeeds();
  622.     
  623.     err = ISpConfigure(nil);
  624.     if (err)
  625.     {
  626.         printf("••• ISpConfigure returned error (%ld)\n",err);
  627.         return;
  628.     }
  629. }
  630.  
  631.  
  632. static void EnableDeviceClass(OSType deviceClass, Boolean enable)
  633. {
  634.     enum {
  635.         kDeviceList_COUNT = 100
  636.     };
  637.     
  638.     OSStatus err;
  639.     UInt32 count;
  640.     ISpDeviceReference deviceList[kDeviceList_COUNT];
  641.     
  642.     // NOTE: This is not the correct way to handle the list count thing.  We
  643.     // should actually call once with NULL for the device list, malloc a list of
  644.     // that size, and call again.
  645.     
  646.     err = ISpDevices_ExtractByClass(
  647.             deviceClass,
  648.             kDeviceList_COUNT,
  649.             &count,
  650.             deviceList);
  651.     
  652.     if (err)
  653.     {
  654.         printf("••• ISpDevices_ExtractByClass returned error (%ld)\n",err);
  655.         return;
  656.     }
  657.     
  658.     if (count > kDeviceList_COUNT)
  659.     {
  660.         count = kDeviceList_COUNT;
  661.     }
  662.     
  663.     if (enable)
  664.     {
  665.         err = ISpDevices_Activate(
  666.                 count,
  667.                 deviceList);
  668.         
  669.         if (err)
  670.         {
  671.             printf("••• ISpDevices_Activate returned error (%ld)\n",err);
  672.             return;
  673.         }
  674.     }
  675.     else
  676.     {
  677.         err = ISpDevices_Deactivate(
  678.                 count,
  679.                 deviceList);
  680.         
  681.         if (err)
  682.         {
  683.             printf("••• ISpDevices_Deactivate returned error (%ld)\n",err);
  684.             return;
  685.         }
  686.     }
  687.     
  688.     if (deviceClass == kISpDeviceClass_Keyboard)
  689.     {
  690.         gKeyboardEnabled = enable;
  691.     }
  692. }
  693.  
  694. static void TestAllocateDeallocate(void)
  695. {
  696.     OSErr err = noErr;
  697.     UInt32 count = 0;
  698.     char theString[32];
  699.     UInt32 itr;    
  700.     ISpElementReference virtuals[kNeedCount];
  701.     Boolean done = false;
  702.     
  703.     printf("allocate/dellocate how many times>");
  704.     
  705.     gets( theString );
  706.     count = atoi( theString );
  707.  
  708.     printf("allocating/deallocating %ld times\n",count);
  709.     
  710.     for(itr = 0; itr < count; itr++)
  711.     {
  712.         if ((itr % 100) == 0) { putchar('.'); fflush(stdout);  }
  713.         if ((itr % 1000) == 0) 
  714.         {
  715.             long normal = FreeMem();
  716.             long system = FreeMemSys();
  717.             printf("%ld / %ld (app = %ld) (sys = %ld) \n",itr,count,normal,system);
  718.         }
  719.     
  720.         SIOUXHandleOneEvent(nil);
  721.         
  722.           err = ISpElement_NewVirtualFromNeeds(kNeedCount, gNeeds, virtuals, 0);
  723.         
  724.         if (err)
  725.         {
  726.             printf("••• itr = %d ISpElement_NewVirtualFromNeeds returned error (%ld)\n",itr, err);
  727.             
  728.             done = true;
  729.         }
  730.     
  731.         err = ISpElement_DisposeVirtual(kNeedCount, virtuals);
  732.     
  733.         if (err)
  734.         {
  735.             printf("••• ISpElementList_Dispose returned error (%ld)\n",err);
  736.             return;
  737.         }
  738.         
  739.         if (done) { return; }
  740.     }
  741. }
  742.  
  743. #define MAX_ELEMENTS    1000
  744. #define MAX_DATASIZE    1000
  745.  
  746.  
  747. #if DEBUG_DRIVER
  748. static void TestAllocatePushData(void)
  749. {
  750.     char theString[32];
  751.     OSErr err = noErr;
  752.     UInt32 elementCount = 0;
  753.     UInt32 dataSize;
  754.     UInt32 count;
  755.     UInt32 elemItr, itr;
  756.     ISpElementReference elements[MAX_ELEMENTS];
  757.     UInt8 state[MAX_DATASIZE];
  758.     
  759.     printf("how many elements>");
  760.     gets( theString );
  761.     elementCount = atoi( theString );
  762.  
  763.     printf("data size>");
  764.     gets( theString );
  765.     dataSize = atoi( theString );
  766.  
  767.     printf("count>");
  768.     gets( theString );
  769.     count = atoi( theString );
  770.  
  771.     printf("%ld elements, data size = %ld, count = %ld\n", elementCount, dataSize, count);
  772.     
  773.     
  774.     if ((elementCount > MAX_ELEMENTS) || (dataSize > MAX_DATASIZE))
  775.     {
  776.         printf("••• MAX_ELEMENTS = %d MAX_DATASIZE = %d\n",MAX_ELEMENTS,MAX_DATASIZE);
  777.         return;
  778.     }
  779.  
  780.     for(elemItr = 0; elemItr < elementCount; elemItr++)
  781.     {
  782.         err = ISpElement_NewVirtual(dataSize, &(elements[elemItr]), 0);
  783.  
  784.         if (err)
  785.         {
  786.             printf("••• ISpElement_NewVirtual returned error (%d)\n",err);
  787.             
  788.             err = ISpElement_DisposeVirtual(elemItr, elements);
  789.             if (err)
  790.             {
  791.                 printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  792.             }
  793.             
  794.             return;
  795.         }
  796.     }
  797.     
  798.     for(itr = 0; itr < count; itr++)
  799.     {
  800.         AbsoluteTime time = {0, TickCount() };
  801.         
  802.         if ((itr % 100) == 0) { printf("."); }
  803.         if ((itr % 1000) == 0) 
  804.         {
  805.             long normal = FreeMem();
  806.             long system = FreeMemSys();
  807.             Ptr temp = NewPtr(1000);
  808.             DisposePtr(temp);
  809.             printf("%ld / %ld (app = %ld) (sys = %ld) \n",itr,count,normal,system);
  810.             
  811.         }
  812.         
  813.         SIOUXHandleOneEvent(nil);
  814.         
  815.         for(elemItr = 0; elemItr < elementCount; elemItr++)
  816.         {
  817.             err = ISpElement_PushComplexData(elements[elemItr], dataSize, &state, &time);
  818.             
  819.             if (err)
  820.             {
  821.                 printf("••• ISpElement_PushComplexData returned error (%d)\n",err);
  822.                 return;
  823.             }
  824.         }
  825.     }
  826.  
  827.     printf("\ndisposing elements\n");
  828.     err = ISpElement_DisposeVirtual(elementCount, elements);
  829.     if (err)
  830.     {
  831.         printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  832.     }
  833. }
  834. #endif /* DEBUG_DRIVER */
  835.  
  836. static void TestArbitraryAllocate(void)
  837. {
  838.     char theString[32];
  839.     OSErr err = noErr;
  840.     UInt32 elementCount = 0;
  841.     UInt32 dataSize;
  842.     UInt32 elemItr;
  843.     ISpElementReference elements[MAX_ELEMENTS];
  844.     
  845.     printf("how many elements>");
  846.     gets( theString );
  847.     elementCount = atoi( theString );
  848.  
  849.     printf("data size>");
  850.     gets( theString );
  851.     dataSize = atoi( theString );
  852.  
  853.  
  854.     printf("%ld elements, data size = %ld\n", elementCount, dataSize);    
  855.     
  856.     if ((elementCount > MAX_ELEMENTS) || (dataSize > MAX_DATASIZE))
  857.     {
  858.         printf("••• MAX_ELEMENTS = %d MAX_DATASIZE = %d\n",MAX_ELEMENTS,MAX_DATASIZE);
  859.         return;
  860.     }
  861.  
  862.     printf("allocating elements\n");
  863.     for(elemItr = 0; elemItr < elementCount; elemItr++)
  864.     {
  865.         err = ISpElement_NewVirtual(dataSize, &(elements[elemItr]), 0);
  866.  
  867.         if (err)
  868.         {
  869.             printf("••• ISpElement_NewVirtual returned error (%d)\n",err);
  870.             
  871.             err = ISpElement_DisposeVirtual(elemItr, elements);
  872.             if (err)
  873.             {
  874.                 printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  875.             }
  876.             
  877.             return;
  878.         }
  879.     }
  880.  
  881.     printf("\ndisposing elements\n");
  882.     err = ISpElement_DisposeVirtual(elementCount, elements);
  883.     if (err)
  884.     {
  885.         printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  886.     }
  887. }
  888.  
  889. #define MTemp 0x828
  890. #define RawMouse 0x82c
  891. #define CrsrNewCouple 0x8ce
  892.  
  893.  
  894.  
  895.  
  896.  
  897. void TestAcceleration(void);
  898. void TestAcceleration(void)
  899. {
  900.     TestCreateNeeds();
  901.  
  902.     ShowCursor();
  903.     Point where =  * ( Point * ) RawMouse;
  904.  
  905.     float deltaX = 0;
  906.     float deltaY = 0;
  907.     
  908.     while(1)
  909.     {
  910.         gVirtualElements[0];
  911.         gVirtualElements[1];
  912.         
  913.         UInt32 forward;
  914.         UInt32 side;
  915.         OSStatus status;
  916.         float temp;
  917.         SInt8 amt;
  918.         
  919.         status = ISpElement_GetSimpleState(gVirtualElements[1], &forward);
  920.  
  921.         if (status == noErr)
  922.         {
  923.             temp = forward;
  924.             temp -= kISpAxisMiddle;
  925.             temp /= kISpAxisMiddle;
  926.             temp *= 25;
  927.             
  928.             deltaX += temp;
  929.             
  930.             if (deltaX >= 1) 
  931.             {
  932.                 amt = deltaX;
  933.                 where.h += amt;
  934.                 deltaX -= amt;
  935.             }
  936.             else if (deltaX <= -1)
  937.             {
  938.                 amt = -deltaX;
  939.                 where.h -= amt;
  940.                 deltaX += amt;
  941.             }
  942.         }
  943.         
  944.         status = ISpElement_GetSimpleState(gVirtualElements[0], &side);
  945.  
  946.         if (status == noErr)
  947.         {
  948.             temp = side;
  949.             temp -= kISpAxisMiddle;
  950.             temp /= kISpAxisMiddle;
  951.             temp *= -25;
  952.             
  953.             deltaY += temp;
  954.  
  955.             if (deltaY >= 1) 
  956.             {
  957.                 amt = deltaY;
  958.                 where.v += amt;
  959.                 deltaY -= amt;
  960.             }
  961.             else if (deltaY <= -1)
  962.             {
  963.                 amt = -deltaY;
  964.                 where.v -= amt;
  965.                 deltaY += amt;
  966.             }
  967.  
  968.         }
  969.                 
  970.         HideCursor();
  971.         
  972.         * ( Point * ) RawMouse = where ;
  973.         * ( Point * ) MTemp = where ;
  974.         * ( short * ) CrsrNewCouple = -1 ;
  975.         
  976.         ShowCursor();
  977.     
  978.         KeyMap theKeys;
  979.         GetKeys(theKeys);
  980.  
  981.         if ((theKeys[1] & 0x8000))
  982.         {
  983.             break;
  984.         }
  985.         
  986.         unsigned long aLong = ::TickCount();
  987.         
  988.         while (aLong == TickCount()) { ; }
  989.     }
  990. }
  991.  
  992. void main(void)
  993. {
  994.     // tell SIOUX to shut up
  995.     SIOUXSettings.autocloseonquit = true;
  996.     SIOUXSettings.asktosaveonclose = false;
  997.     
  998.     printf("starting up...\n");
  999.     
  1000.     Boolean theDoneFlag = false;
  1001.     
  1002.     while( !theDoneFlag )
  1003.     {
  1004.         UInt32 theChoice;
  1005.         
  1006.         printf("\n\n\n############################################################\n");
  1007.         if (gIsSuspended)
  1008.         {
  1009.             printf("• SUSPENDED •\n");
  1010.         }
  1011.         
  1012.         if (gVirtualList != NULL)
  1013.         {
  1014.             printf("• NEEDS ARE ACTIVE •\n");
  1015.         }
  1016.         
  1017.         printf("Please select from the following tests:\n");        
  1018.         printf("\t 1. Exit\n");
  1019. //        printf("\t 2. Run all tests automatically\n");
  1020. //        printf("\t 3. Dump information needed for InputSprocket bug reports\n");
  1021.         printf("\t 4. List all devices\n");
  1022.         printf("\t 5. List all elements\n");
  1023.         printf("\t 6. List all buttons\n");
  1024.         printf("\t 7. List all directional pads\n");
  1025.         printf("\t 8. List all axes\n");
  1026.         printf("\t 9. List all X-axes\n");
  1027.         printf("\t10. Get events (stop when Control key pressed)\n");
  1028.         printf("\t11. Suspend\n");
  1029.         printf("\t12. Resume\n");
  1030.         printf("\t13. Create needs\n");
  1031.         printf("\t14. Destroy needs\n");
  1032.         printf("\t15. Get needs events\n");
  1033.         printf("\t16. Configuration dialog\n");
  1034.         printf("\t17. Enable mouse and keyboard\n");
  1035.         printf("\t18. Disable mouse and keyboard\n");
  1036.         printf("\t19. Reinitialize ADB bus\n");
  1037.         printf("\t30. Allocate/Deallocate test\n");
  1038.         #if DEBUG_DRIVER
  1039.             printf("\t31. TestAllocatePushData test\n");
  1040.         #endif
  1041.         printf("\t32. TestArbitraryAllocate test\n");
  1042.         printf("\t33. TestAccelration\n");
  1043.         
  1044.         printf("\n\tSelection: ");
  1045.         
  1046.         Boolean keyboardEnabled = gKeyboardEnabled;
  1047.         if (keyboardEnabled)
  1048.         {
  1049.             EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  1050.         }
  1051.         
  1052.         char theString[32];
  1053.         gets( theString );
  1054.         theChoice = atoi( theString );
  1055.         
  1056.         if (keyboardEnabled)
  1057.         {
  1058.             EnableDeviceClass(kISpDeviceClass_Keyboard, true);
  1059.         }
  1060.         
  1061.         printf("\n\n");
  1062.         
  1063.         switch( theChoice )
  1064.         {
  1065.             case 1:
  1066.                 theDoneFlag = true;
  1067.                 break;
  1068.                 
  1069.             case 4:
  1070.                 TestListOfDevices();
  1071.                 break;            
  1072.             
  1073.             case 5:
  1074.                 TestListOfElements();
  1075.                 break;            
  1076.             
  1077.             case 6:
  1078.                 TestListOfButtons();
  1079.                 break;            
  1080.             
  1081.             case 7:
  1082.                 TestListOfDPads();
  1083.                 break;            
  1084.             
  1085.             case 8:
  1086.                 TestListOfAxes();
  1087.                 break;            
  1088.             
  1089.             case 9:
  1090.                 TestListOfXAxes();
  1091.                 break;            
  1092.             
  1093.             case 10:
  1094.                 GetAndPrintEvents(NULL);
  1095.                 break;            
  1096.             
  1097.             case 11:
  1098.                 TestSuspend();
  1099.                 break;            
  1100.             
  1101.             case 12:
  1102.                 TestResume();
  1103.                 break;            
  1104.             
  1105.             case 13:
  1106.                 TestCreateNeeds();
  1107.                 break;            
  1108.             
  1109.             case 14:
  1110.                 TestDestroyNeeds();
  1111.                 break;            
  1112.             
  1113.             case 15:
  1114.                 TestNeedsEvents();
  1115.                 break;            
  1116.             
  1117.             case 16:
  1118.                 TestConfiguration();
  1119.                 break;            
  1120.             
  1121.             case 17:
  1122.                 EnableDeviceClass(kISpDeviceClass_Mouse, true);
  1123.                 EnableDeviceClass(kISpDeviceClass_Keyboard, true);
  1124.                 break;            
  1125.             
  1126.             case 18:
  1127.                 EnableDeviceClass(kISpDeviceClass_Mouse, false);
  1128.                 EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  1129.                 break;
  1130.             
  1131.             case 19:
  1132.                 ADBReInit();
  1133.                 break;
  1134.                 
  1135.             case 30:
  1136.                 TestAllocateDeallocate();
  1137.                 break;
  1138.         
  1139.         #if DEBUG_DRIVER
  1140.             case 31:
  1141.                 TestAllocatePushData();
  1142.                 break;
  1143.         #endif
  1144.             
  1145.             case 32:
  1146.                 TestArbitraryAllocate();
  1147.                 break;
  1148.             
  1149.             case 33:
  1150.                 TestAcceleration();
  1151.                 break;
  1152.                 
  1153.             default:
  1154.                 printf("\nhuh?\n");
  1155.                 break;            
  1156.         }
  1157.         
  1158.         printf("\nThank you, please drive through.\n");
  1159.     }
  1160. }
  1161.